//
//  WaveView.m
//  WaveViewDemo
//
//  Created by Darwin on 2017/4/6.
//  Copyright © 2017年 Darwin. All rights reserved.
//

#import "WaveView.h"
#import "TargetWeakProxy.h"

@interface WaveView ()

/*
 * 波动的宽度
 */
@property (nonatomic, assign) CGFloat waveWidth;

/*
 * 设置位移
 */
@property (nonatomic, assign) CGFloat fristOffset;

@property (nonatomic, assign) CGFloat secondOffset;

@property (nonatomic, assign) CGFloat thirdOffset;

/*
 * 可变量变化相关
 */
@property (nonatomic, assign) CGFloat variable;
@property (nonatomic, assign) BOOL isIncrease;

/*
 * 两个图层
 */
@property (nonatomic, strong) CAShapeLayer *firstWaveLayer;
@property (nonatomic, strong) CAShapeLayer *secondWaveLayer;
@property (nonatomic, strong) CAShapeLayer *thirdWaveLayer;
/*
 * 定时器
 */
@property (nonatomic, strong) CADisplayLink *waveOneDisplayLink;

@property (nonatomic, strong) CADisplayLink *waveTwoDisplayLink;

@property (nonatomic, strong) CADisplayLink *waveThreeDisplayLink;

//@property (nonatomic, strong) NSTimer *firstTimer;
//@property (nonatomic, strong) NSTimer *secondTimer;
//@property (nonatomic, strong) NSTimer *thirdTimer;
/*
 * 是否正在波动
 */
@property (nonatomic, assign) BOOL isWaving;

/*
 * 是否是手动设置的振幅
 */
@property (nonatomic, assign) BOOL isCustomAmplitude;

@end

@implementation WaveView



- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    _waveWidth = frame.size.width;
    
    if (_waveWidth > 0) {
        _waveCycle = 1.29 * M_PI / _waveWidth;
    }
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.masksToBounds = YES;
        [self setupProperty];
        [self startWave];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.masksToBounds = YES;
        [self setupProperty];
        [self startWave];
    }
    return self;
}

- (void)setupProperty
{
    _fristOffset = 0;
    _secondOffset = 0;
    _thirdOffset = 0;
    _waveSpeed = -1;
    _variable = 1.6;
    _isIncrease = NO;
    _isDouble = NO;
    _isMutable = YES;
    _isWaving = NO;
    _isCustomAmplitude = NO;
}

- (void)setFirsetWaveColor:(UIColor *)firsetWaveColor {
    _firsetWaveColor = firsetWaveColor;
    _firstWaveLayer.fillColor = firsetWaveColor.CGColor;
}

- (void)setSecondWaveColor:(UIColor *)secondWaveColor {
    _secondWaveColor = secondWaveColor;
    _secondWaveLayer.fillColor = secondWaveColor.CGColor;
}
- (void)setThirdWaveColor:(UIColor *)thirdWaveColor
{
    _thirdWaveColor = thirdWaveColor;
    _thirdWaveLayer.fillColor = thirdWaveColor.CGColor;
}
- (void)startWave {
    
    if(_isWaving) {
        [self stopWave];
    }
    _isWaving = YES;
    if(_firstWaveLayer == nil) {
        _firstWaveLayer = [CAShapeLayer layer];
        _firstWaveLayer.fillColor = _firsetWaveColor.CGColor;
        [self.layer insertSublayer:_firstWaveLayer atIndex:2];
    }
    
    if(_secondWaveLayer == nil) {
        _secondWaveLayer = [CAShapeLayer layer];
        _secondWaveLayer.fillColor = _secondWaveColor.CGColor;
        [self.layer insertSublayer:_secondWaveLayer atIndex:1];
    }
    if(_thirdWaveLayer == nil) {
        _thirdWaveLayer = [CAShapeLayer layer];
        _thirdWaveLayer.fillColor = _thirdWaveColor.CGColor;
        [self.layer insertSublayer:_thirdWaveLayer atIndex:0];
    }
    
    
//
//    _firstTimer = [NSTimer timerWithTimeInterval:1/60.f target:self selector:@selector(getFirstWave) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop]addTimer:_firstTimer forMode:NSDefaultRunLoopMode];
//
//
//    _secondTimer = [NSTimer timerWithTimeInterval:1/60.0f target:self selector:@selector(getSecondWave) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop]addTimer:_secondTimer forMode:NSDefaultRunLoopMode];
//
//    _thirdTimer = [NSTimer timerWithTimeInterval:1/60.0f target:self selector:@selector(gethirdWave) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop]addTimer:_thirdTimer forMode:NSDefaultRunLoopMode];
    

    if(_waveOneDisplayLink == nil) {
        _waveOneDisplayLink = [CADisplayLink displayLinkWithTarget:[TargetWeakProxy proxyWithTarget:self] selector:@selector(getFirstWave)];
        [_waveOneDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        _waveOneDisplayLink.frameInterval = 1;

    }

    if(_waveTwoDisplayLink == nil) {
        _waveTwoDisplayLink = [CADisplayLink displayLinkWithTarget:[TargetWeakProxy proxyWithTarget:self] selector:@selector(getSecondWave)];
        [_waveTwoDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        _waveTwoDisplayLink.frameInterval = 1;

    }

    if(_waveThreeDisplayLink == nil) {
        _waveThreeDisplayLink = [CADisplayLink displayLinkWithTarget:[TargetWeakProxy proxyWithTarget:self] selector:@selector(gethirdWave)];
        [_waveThreeDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        _waveThreeDisplayLink.frameInterval = 1;

    }
}


- (void)gethirdWave
{
    _secondOffset += 1.5 / 60;
    [self waveSetVariable];
    [self drawWaveThirdSinPathInLayer:_thirdWaveLayer];
}

- (void)getSecondWave
{
    _thirdOffset += 1.2 / 60;
    [self waveSetVariable];
    [self drawWaveCosPathInLayer:_secondWaveLayer];
}

- (void)getFirstWave {
    
    _fristOffset += -1.0f / 60.f;
    [self waveSetVariable];
    [self drawWaveSinPathInLayer:_firstWaveLayer];
   
  
}
- (void)drawWaveThirdSinPathInLayer:(CAShapeLayer *)shapeLayer {
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGFloat x = 0.0;
    CGFloat y = 0.0;
    CGPathMoveToPoint(path, nil, x, y);
    for ( ; x <= self.bounds.size.width; x++) {
        y = _waveAmplitude / 2 * sinf(1.29 * M_PI / _waveWidth* x  + _thirdOffset + 60) + _waveAmplitude / 2;
        CGPathAddLineToPoint(path, nil, x, y);
    }
    CGPathAddLineToPoint(path, nil, self.bounds.size.width, self.bounds.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.bounds.size.height);
    CGPathCloseSubpath(path);
    shapeLayer.path = path;
    CGPathRelease(path);
    
}
- (void)drawWaveSinPathInLayer:(CAShapeLayer *)shapeLayer {
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat x = 0;
    CGFloat y = 0.0;
    CGPathMoveToPoint(path, nil, x, y);
    for ( ; x <= self.bounds.size.width; x++) {
            y = _waveAmplitude / 2 * sinf(1.29 * M_PI / _waveWidth *1.1 * x + _secondOffset) + _waveAmplitude / 2;
            CGPathAddLineToPoint(path, nil, x, y);
    }
    CGPathAddLineToPoint(path, nil, self.bounds.size.width, self.bounds.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.bounds.size.height);
    CGPathCloseSubpath(path);
    shapeLayer.path = path;
    CGPathRelease(path);
}
- (void)drawWaveCosPathInLayer:(CAShapeLayer *)shapeLayer {
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat x = 0.0;
    CGFloat y = 0.0;
    CGPathMoveToPoint(path, nil, x, y);
    for ( ; x <= self.bounds.size.width; x++) {
        y = _waveAmplitude / 2 * sinf(1.29 * M_PI /_waveWidth * x + _fristOffset + 30) + _waveAmplitude / 2;
        CGPathAddLineToPoint(path, nil, x, y);
    }
    CGPathAddLineToPoint(path, nil, self.bounds.size.width, self.bounds.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.bounds.size.height);
    CGPathCloseSubpath(path);
    shapeLayer.path = path;
    CGPathRelease(path);
}

- (void)waveSetVariable {
    
    if(_isIncrease) {
        _variable += 0.01;
    } else {
        _variable -= 0.01;
    }
    
    if(_variable <= 1) {
        _isIncrease = YES;
    }
    
    if(_variable >= 1.6) {
        _isIncrease = NO;
    }
    
    if(_isCustomAmplitude == NO) {
        
        if(_isMutable) {
            _waveAmplitude = _variable * 5 * 2;
        } else {
            _waveAmplitude = 1.6 * 5 * 2;
        }
        
    }
}

- (void)setWaveAmplitude:(CGFloat)waveAmplitude {
    _waveAmplitude = waveAmplitude;
    
    if(waveAmplitude == -1) {
        _isCustomAmplitude = NO;
    }else {
        _isCustomAmplitude = YES;
    }
}

- (void)stopWave {
    
    _isWaving = NO;
    
//    [_firstTimer invalidate];
//    [_secondTimer invalidate];
//    [_thirdTimer invalidate];
    if(_waveOneDisplayLink) {
        [_waveOneDisplayLink invalidate];
        _waveOneDisplayLink = nil;
    }
    
    if(_waveTwoDisplayLink) {
        [_waveTwoDisplayLink invalidate];
        _waveTwoDisplayLink = nil;
    }
    
    if(_waveThreeDisplayLink) {
        [_waveThreeDisplayLink invalidate];
        _waveThreeDisplayLink = nil;
    }
    
    if(_firstWaveLayer) {
        [_firstWaveLayer removeFromSuperlayer];
        _firstWaveLayer = nil;
    }
    
    if(_secondWaveLayer) {
        [_secondWaveLayer removeFromSuperlayer];
        _secondWaveLayer = nil;
    }
}

- (void)setIsDouble:(BOOL)isDouble {
    _isDouble = isDouble;
    if(isDouble == NO) {
        _secondWaveLayer.fillColor = [UIColor clearColor].CGColor;
    } else {
        _secondWaveLayer.fillColor = self.secondWaveColor.CGColor;
    }
}

@end
